package service.impl;

import dao.IScheduleDao;
import dao.impl.ScheduleDaoImpl;
import entity.Movie;
import entity.Schedule;
import service.IMovieService;

import java.util.ArrayList;
import java.util.List;

public class ScheduleServiceImpl implements service.IScheduleService {
    private final IScheduleDao scheduleDao = new ScheduleDaoImpl();
    private final service.IMovieService movieService = new MovieServiceImpl();
    private List<Schedule> schedules;

    public ScheduleServiceImpl() {
        schedules = scheduleDao.load();
    }

    @Override
    public List<Schedule> findAll() {
        return schedules;
    }

    @Override
    public boolean add(Schedule schedule) {
        schedules.add(schedule);
        scheduleDao.save(schedules);
        return true;
    }

    @Override
    public boolean delete(String id) {
        boolean result = schedules.removeIf(s -> s.getId().equals(id));
        if (result) {
            scheduleDao.save(schedules);
        }
        return result;
    }

    @Override
    public List<Schedule> findByMovieId(String movieId) {
        return schedules.stream().filter(s -> s.getMovieId().equals(movieId)).toList();
    }

    @Override
    public List<Schedule> findByDate(java.time.LocalDateTime date) {
        return schedules.stream()
                .filter(s -> s.getStartTime().toLocalDate().equals(date.toLocalDate()))
                .toList();
    }

    @Override
    public void autoSchedule() {
        // 实现自动排片逻辑
    }

    @Override
    public void shutdown() {
        scheduleDao.save(schedules);
    }

    @Override
    public Schedule findById(String id) {
        return schedules.stream().filter(s -> s.getId().equals(id)).findFirst().orElse(null);
    }
}